package com.funny.test.config;

import com.funny.encrypt.interceptor.ParameterEncryptInterceptor;
import com.funny.encrypt.interceptor.ResultSetDecryptInterceptor;
import com.funny.mapper.AppMapper;
import com.funny.mapper.LocalTestMapper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * @author fanxiao  2021/8/16
 * @since 1.0.0
 */
@Configuration
@ComponentScan(basePackages = {"com.funny.service"})
@MapperScan("com.funny.mapper")
public class DataSourceConfig {

	@Bean
	public DataSource dataSource(){
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://10.82.29.30:3306/auth?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true");
		dataSource.setUsername("anmi");
		dataSource.setPassword("AFE#qr33");
		return dataSource;
	}

	@Bean
	public ParameterEncryptInterceptor parameterEncryptInterceptor() {
		return new ParameterEncryptInterceptor();
	}

	@Bean
	public ResultSetDecryptInterceptor resultSetDecryptInterceptor() {
		return new ResultSetDecryptInterceptor();
	}



	@Bean
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dataSource());
		factoryBean.setPlugins(parameterEncryptInterceptor(), resultSetDecryptInterceptor());
		return factoryBean.getObject();
	}



}
